<?php

/**
 * main actions.
 *
 * @package    appcemhti
 * @subpackage main
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainActions extends sfActions
{
    /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex(sfWebRequest $request)
    {
    }
    
    public function executeListLot(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            
            $q = Doctrine_Core::getTable('lot')->getLotParUtilisateurParDroitDatatable($this->getUser()->getGuardUser()->getId(), "lecture", $sSearch, $iSortCol_0, $sSortDir_0);
            
            $this->pager = new sfDoctrinePager(
                'lot',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);

            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url', 'Date');
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {
                $consulter = '<a class="submit" href="lot/show/id_lot/' . $v->getIdLot() . '">Consulter</a>';
                if($v->getConfidentiel() == 1) $conf = "Oui"; else $conf = "Non";
                $fournisseur = "";
                if($v->getIdFournisseur()) $fournisseur = $v->getFournisseur()->getNom();
                $user = $v->getUser()->getSfGuardUser();
                $date = "";
                if($v->getDateRecep()) $date = $v->getDateTimeObject('date_recep')->format('d/m/Y');
                $aaData[] = array(
                    "0" => $v->getIdLot(),
                    "1" => $v->getNom(),
                    "2" => $v->getMatiere()->getLibelle(),
                    "3" => $v->getPolytype(),
                    "4" => $v->getEchantillon()->count(),
                    "5" => $fournisseur,
                    "6" => $date,
                    "7" => $user->getFirstName() . " " . $user->getLastName(),
                    "8" => $conf,
                    "9" => $v->getCommentaire()->count(),
                    "10" => $consulter,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }

    
    public function executeListEchantillon(sfWebRequest $request)
    {
        // Vérification requête AJAX
        if ($request->isXmlHttpRequest())
        {
            // Récupération des paramètres
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            // Création de la requète
            $q = Doctrine_Core::getTable('Echantillon')->getEchantillonParUtilisateurParDroitDatatable(
                $this->getUser()->getGuardUser()->getId(),
                "lecture",
                $sSearch,
                $iSortCol_0,
                $sSortDir_0
            );
            // Création du pager
            $this->pager = new sfDoctrinePager('echantillon', sfConfig::get(2));
            //Le pager récupère la requète
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            $pager = $this->pager;
            $list = $pager->getResults();
            // Création de la liste d'item à renvoyer
            $aaData = array();
            foreach ($list as $v)
            {
                $forme = "";
                if($v->getTypeForme()) $forme = $v->getTypeForme()->getLibelle();
                $lieu = "";
                if($v->getLieu()) $lieu = $v->getLieu()->getLibelle();
                $date = "";
                if($v->getDateCreation()) $date = $v->getDateTimeObject('date_creation')->format('d/m/Y');
                $consulter = '<a class="submit" href="echantillon/show/id_echantillon/'
                    .$v->getIdEchantillon()
                    .'">Consulter</a>';
                $aaData[] = array(
                    "0" => $v->getIdEchantillon(),
                    "1" => $v->getNom(),
                    "2" => $v->getLot()->getNom(),
                    "3" => $date,
                    "4" => $v->getEtatEchantillon()->getLibelle(),
                    "5" => $lieu,
                    "6" => $forme,
                    "7" => $v->getNbTraitement(),
                    "8" => $v->getMesure()->count(),
                    "9" => $v->getCommentaire()->count(),
                    "10" => $consulter,
                );
            }
            // Formatage pour le datatable de la chaine de sortie
            $output = array(
                "iTotalRecords" => count($pager),
                "iTotalDisplayRecords" => count($pager),
                "aaData" => $aaData,
            );
            return $this->renderText(json_encode($output));
        }
    }

}
